<?php
namespace wechat\controllers;

use Yii;
use yii\base\Event;
use yii\helpers\Url;
use framework\helpers\Utils;
use yii\web\Controller;

/**
 * Site controller
 */
class AuthenticationController extends Controller
{
	public $layout = 'layout';
    public function actionIndex()
    {
    	$session = yii::$app->session;
    	$session->open();
    	if($session->has('sKey'))	//是否有sKey缓存
    	{
    		$sKey = $session->get('sKey');
    		$url = PANDA_SERVER.'/getUserSecurityInfo?sKey='.$sKey.'&tokenId='.Utils::getTokenId();
    		$result = file_get_contents($url);
    		$result_arr = json_decode($result,true); // JSON结果字符串转数组
    		if ($result_arr['status']!=0)	//sKey过期
    		{
    			return $this->redirect(['site/signin']);
    		}
    		else 	//已登录
    		{
    			return $this->render('index');
    		}
    	}
    	else
    	{
    		return $this->redirect(['site/signin']);
    	}
    }
    
    public function actionDoauthentication()
    {
    	$session = yii::$app->session;
    	$session->open();
    	if($session->has('sKey'))	//是否有sKey缓存
    	{
    		$sKey = $session->get('sKey');
    		$url = PANDA_SERVER.'/getUserSecurityInfo?sKey='.$sKey.'&tokenId='.Utils::getTokenId();
    		$result = file_get_contents($url);
    		$result_arr = json_decode($result,true); // JSON结果字符串转数组
    		if ($result_arr['status']!=0)	//sKey过期
    		{
    			return $this->redirect(['site/signin']);
    		}
    		else 	//已登录
    		{
    			if ($_POST )
    			{
    				//接收POST过来的参数
    				$realname = $_POST['realname'];
    				$idcard = $_POST['idcard'];
    				$front = $_POST['front'];
    				$back = $_POST['back'];
    				$sKey = $_POST['sKey'];
    				
    				$url = PANDA_SERVER.'/submitAuthID?sKey='.$sKey.'&realname='.$realname.'&IDCard='.$idcard.'&front='.$front.'&back='.$back.'&tokenId='.Utils::getTokenId();  //将用户填写的真实姓名、身份证号、身份证（正、反）图片保存并提交到后台，待审核。
    				
    				$result = file_get_contents($url);
    				echo $result;
    				exit;
    			}
    		}
    	}
    	else
    	{
    		return $this->redirect(['site/signin']);
    	}
    }
    //发送二进制流图片
    function sendStreamFile($url, $file){
    
    	if(file_exists($file)){
    
    		$opts = array(
    				'http' => array(
    						'method' => 'POST',
    						'header' => 'content-type:application/x-www-form-urlencoded',
    						'content' => file_get_contents($file)
    				)
    		);
    
    		$context = stream_context_create($opts);
    		$response = file_get_contents($url, false, $context);
    		$ret = json_decode($response, true);
    		return $ret['success'];
    
    	}else{
    		return false;
    	}
    
    }
    function Post($url, $post = null) {
    	if (is_array($post)) {
    		ksort($post);
    		$content = http_build_query($post);
    		$content_length = strlen($content);
    		$options = array(
    				'http' => array(
    						'method' => 'POST',
    						'header' =>
    						"Content-type: application/x-www-form-urlencoded\r\n" .
    						"Content-length: $content_length\r\n",
    						'content' => $content
    				)
    		);
    		return file_get_contents($url, false, stream_context_create($options));
    	}
    }
}